package com.hskn.hss.module.messageFlowRecords.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.messageFlowRecords.domain.MessageFlowRecords;
import com.hskn.hss.module.messageFlowRecords.mapper.MessageFlowRecordsMapper;
import com.hskn.hss.module.messageFlowRecords.service.IMessageFlowRecordsService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 消息流水Service业务层处理
 * 
 * @author ccm
 * @date 2021-09-14
 */
@Service
public class MessageFlowRecordsServiceImpl extends ServiceImpl<MessageFlowRecordsMapper, MessageFlowRecords> implements IMessageFlowRecordsService
{
    @Resource
    private MessageFlowRecordsMapper messageFlowRecordsMapper;

    /**
     * 查询消息流水
     * 
     * @param id 消息流水主键
     * @return 消息流水
     */
    @Override
    public MessageFlowRecords selectMessageFlowRecordsById(String id)
    {
        return messageFlowRecordsMapper.selectMessageFlowRecordsById(id);
    }

    /**
     * 查询消息流水列表
     * 
     * @param messageFlowRecords 消息流水
     * @return 消息流水
     */
    @Override
    public List<MessageFlowRecords> selectMessageFlowRecordsList(MessageFlowRecords messageFlowRecords)
    {
        return messageFlowRecordsMapper.selectMessageFlowRecordsList(messageFlowRecords);
    }

    /**
     * 新增消息流水
     * 
     * @param messageFlowRecords 消息流水
     * @return 结果
     */
    @Override
    public int insertMessageFlowRecords(MessageFlowRecords messageFlowRecords)
    {
        messageFlowRecords.setId(StringUtils.uuid());
        messageFlowRecords.setCreateTime(new Date());
        return messageFlowRecordsMapper.insertMessageFlowRecords(messageFlowRecords);
    }
    /**
     * 唯一新增消息流水
     *
     * @param messageFlowRecords 消息流水
     * @return 结果
     */
    @Override
    public int insertMessageFlowRecordsOnly(MessageFlowRecords messageFlowRecords)
    {
        messageFlowRecords.setId(StringUtils.uuid());
        messageFlowRecords.setCreateTime(new Date());
        return messageFlowRecordsMapper.insertMessageFlowRecordsOnly(messageFlowRecords);
    }

    /**
     * 修改消息流水
     * 
     * @param messageFlowRecords 消息流水
     * @return 结果
     */
    @Override
    public int updateMessageFlowRecords(MessageFlowRecords messageFlowRecords)
    {
        return messageFlowRecordsMapper.updateMessageFlowRecords(messageFlowRecords);
    }

    /**
     * 批量删除消息流水
     * 
     * @param ids 需要删除的消息流水主键
     * @return 结果
     */
    @Override
    public int deleteMessageFlowRecordsByIds(String[] ids)
    {
        return messageFlowRecordsMapper.deleteMessageFlowRecordsByIds(ids);
    }

    /**
     * 删除消息流水信息
     * 
     * @param id 消息流水主键
     * @return 结果
     */
    @Override
    public int deleteMessageFlowRecordsById(String id)
    {
        return messageFlowRecordsMapper.deleteMessageFlowRecordsById(id);
    }

    /**
     * 是否已读
     *
     * @return 结果
     */
    @Override
    public Boolean isRead(String userId, String messageId)
    {
        Boolean isRead = false;
        QueryWrapper<MessageFlowRecords> messageFlowRecordsQueryWrapper = new QueryWrapper<>();
        messageFlowRecordsQueryWrapper.lambda().eq(MessageFlowRecords::getUserId, userId);
        messageFlowRecordsQueryWrapper.lambda().eq(MessageFlowRecords::getMessageId, messageId);
        List<MessageFlowRecords> messageFlowRecords = messageFlowRecordsMapper.selectList(messageFlowRecordsQueryWrapper);
        if (Objects.nonNull(messageFlowRecords) && messageFlowRecords.size() > 0){
            isRead = true;
        }
        return isRead;
    }

    /**
     * 是否已读
     *
     * @return 结果
     */
    @Override
    public Map<String, Map<String, MessageFlowRecords>> getMessageFlowRecordsByUserIdAndMessageIdMap()
    {
        Map<String, Map<String, MessageFlowRecords>> messageFlowRecordsByUserIdAndMessageMap = new HashMap<>();

        List<MessageFlowRecords> messageFlowRecords = messageFlowRecordsMapper.selectList(new QueryWrapper<>());
        if (Objects.isNull(messageFlowRecords) || messageFlowRecords.size() == 0){
            return null;
        }
        Map<String, List<MessageFlowRecords>> messageFlowRecordsByUserIdMap = messageFlowRecords.stream().filter(a -> StringUtils.isNotEmpty(a.getUserId())).collect(Collectors.groupingBy(MessageFlowRecords::getUserId));

        messageFlowRecordsByUserIdMap.keySet().forEach(t->{
            List<MessageFlowRecords> messageFlowRecords1 = messageFlowRecordsByUserIdMap.get(t);
            Map<String, MessageFlowRecords> messageFlowRecordsByMessageIdMap = messageFlowRecords1.stream().filter(a -> StringUtils.isNotEmpty(a.getMessageId())).collect(Collectors.groupingBy(MessageFlowRecords::getMessageId, Collectors.collectingAndThen(toList(), a -> a.get(0))));
            messageFlowRecordsByUserIdAndMessageMap.put(t, messageFlowRecordsByMessageIdMap);
        });
        return messageFlowRecordsByUserIdAndMessageMap;
    }
}
